/*

表单验证器

示例代码

	var validators = 
	{
		"name": new ruite.form.StringLengthValidator("班级名称", 100),
		"typeId": new ruite.form.SelectValidator("班级类别"),
		"hours": new ruite.form.IntegerValidator("预计课时"),
		"lectureTitle": new ruite.form.StringLengthValidator("讲稿题目", 100),
		"responser": new ruite.form.SelectValidator("负责人")
	};
	
	if (!ruite.form.validate($("class_form"), validators)) return false;
*/

if (!ruite) var ruite = {};
if (!ruite.form) ruite.form = {};

// 几种数据的格式
ruite.form.pattern = {};
ruite.form.pattern.integer = /^-?\d+$/;
ruite.form.pattern.decimal = /^-?\d+(\.\d+)?$/;
ruite.form.pattern.date = /^\d{4}-\d{1,2}-\d{1,2}$/;
ruite.form.pattern.timestamp = /^\d{4}-\d{1,2}-\d{1,2} \d{1,2}:\d{1,2}$/;
ruite.form.pattern.email = /.+@.+\..+/;
ruite.form.pattern.phone = /^((\(\d{2,3}\))|(\d{3}\-))?(\(0\d{2,3}\)|0\d{2,3}-)?[1-9]\d{6,7}(\-\d{1,4})?$/;
ruite.form.pattern.mobile = /^((\(\d{2,3}\))|(\d{3}\-))?13\d{9}$/;
ruite.form.pattern.qq = /^[1-9]\d{4,}$/;
ruite.form.pattern.username = /^[a-z][_a-zA-Z0-9]{0,}$/i ;

/** 验证表单数据的格式 */
ruite.form.validate = function(form, validators)
{
	try
	{
		var elements = form.elements;
		for (var i = 0; ; i++)
		{
			var element = elements[i];
			if (element == null) break;
			var name = element.name;
			if (name == null || name.length < 1) continue;
			
			var validator = validators[name];
			if (validator) validator.validate(element);
		}
		
		return true;
	}
	catch (e)
	{
		alert(e.message);
		if(e.element.disabled)
		{
			if(e.element.disabled == "disabled" || e.element.disabled == true)
			{
				return false;
			}
		}
		e.element.focus();
		
		return false;
	}
}

ruite.form.StringLengthValidator = function (name, length)
{
	this.name = name;
	this.length = length;
	this.validate = function (element)
	{
		var s = ruite.form.trimString(element.value);
		if (s.length < 1 || s.length > this.length)	
			throw new ruite.form.Exception(element, "" + this.name + " 不能为空且长度不能大于 " + this.length);
	}
}

ruite.form.StringEmptyValidator = function(name)
{
	this.name = name;
	this.validate = function(element)
	{
		var s = ruite.form.trimString(element.value);
		if(s.length<1)
			throw new ruite.form.Exception(element,"" + this.name + " 不能为空");
	}
}
ruite.form.IntegerValidator = function (name)
{
	this.name = name;
	this.validate = function (element)
	{
		var s = ruite.form.trimString(element.value);
		if (!ruite.form.pattern.integer.test(s))	
			throw new ruite.form.Exception(element, "" + this.name + " 必须为整数");
	}
}

ruite.form.DecimalValidator = function (name)
{
	this.name = name;
	this.validate = function (element)
	{
		var s = ruite.form.trimString(element.value);
		if (!ruite.form.pattern.decimal.test(s))	
			throw new ruite.form.Exception(element, "" + name + " 必须为数值(整数或浮点数)");
	}
}

ruite.form.DateValidator = function (name)
{
	this.name = name;
	this.validate = function (element)
	{
		var s = ruite.form.trimString(element.value);
		if (!ruite.form.pattern.date.test(s))	
			throw new ruite.form.Exception(element, "" + name + " 必须为日期格式(yyyy-mm-dd)");
	}
}

ruite.form.TimestampValidator = function (name)
{
	this.name = name;
	this.validate = function (element)
	{
		var s = ruite.form.trimString(element.value);
		if (!ruite.form.pattern.timestamp.test(s))	
			throw new ruite.form.Exception(element, "" + this.name + " 必须为时间戳格式(yyyy-mm-dd hh:mm)");
	}
}

ruite.form.SelectValidator = function (name)
{
	this.name = name;
	this.validate = function (element)
	{
		if (element.value * 1 < 1)
			throw new ruite.form.Exception(element, "" + this.name + " 必须选择");
	}
}
ruite.form.SelectEmptyValidator = function (name)
{
	this.name = name;
	this.validate = function (element)
	{
		if (!element.value||element.value=="-1")
			throw new ruite.form.Exception(element, "" + this.name + " 必须选择");
	}
}
ruite.form.LengthValidator = function (name, length)
{
	this.name = name;
	this.length = length;
	this.validate = function (element)
	{
		var s = ruite.form.trimString(element.value);
		if (s.length >0 && s.length > this.length)	
			throw new ruite.form.Exception(element, "" + this.name + " 长度不能大于 " + this.length);
	}
}
ruite.form.GenderValidator = function (name)
{
	this.name = name;
	this.validate = function (element) 
	{
		if(element.value != "男" && element.value != "女")
			throw new ruite.form.Exception(element, "" + this.name + " 只能是 男 或者 女");
	}
}

ruite.form.EmailValidator = function(name)
{
	this.name = name;
	this.validate = function (element)
	{
		var s = ruite.form.trimString(element.value);
		if (s.length >0 && !ruite.form.pattern.email.test(s))	
			throw new ruite.form.Exception(element, "" + this.name + " 必须是正确的电子邮件地址XXX@XXX.XXX");
	}
}

ruite.form.PhoneValidator = function(name)
{
	this.name = name;
	this.validate = function (element)
	{
		var s = ruite.form.trimString(element.value);
		if (s.length >0 && !ruite.form.pattern.phone.test(s))	
			throw new ruite.form.Exception(element, "" + this.name + "格式不正确，正确的格式为0XX-XXXXXXX-XXX");
	}
}

ruite.form.MobileValidator = function(name)
{
	this.name = name;
	this.validate = function (element)
	{
		var s = ruite.form.trimString(element.value);
		if (s.length >0 && !ruite.form.pattern.mobile.test(s))	
			throw new ruite.form.Exception(element, "" + this.name + "格式不正确，必须是数字");
	}
}

ruite.form.qqValidator = function(name)
{
	this.name = name;
	this.validate = function (element)
	{
		var s = ruite.form.trimString(element.value);
		if (s.length >0 && !ruite.form.pattern.qq.test(s))	
			throw new ruite.form.Exception(element, "" + this.name + "格式不正确");
	}
}

ruite.form.UsernameValidator = function(name)
{
	this.name = name;
	this.validate = function (element)
	{
		var s = ruite.form.trimString(element.value);
		if (!ruite.form.pattern.username.test(s))	
			throw new ruite.form.Exception(element, "" + this.name + "格式不正确，必须是字母开头，数字、字母和下划线的组合，且长度大于0");
	}
}
//比较考试的结束时间是否在开始时间之后
ruite.form.compareExamTime = function(startExamTime, endExamTime)
{
	var length = 0;
	//如果开始时间或结束时间为空 ，返回
	if ("" == startExamTime || "" == endExamTime) return length;
	
	var date1 = startExamTime.toString();
	var date2 = endExamTime.toString();
	// 进行处理，将日期和时间分开
	var dateTime1 = date1.split(" ")
	var startDate = dateTime1[0];
	var startTime = dateTime1[1];
	var dateTime2 = date2.split(" ")
	var endDate = dateTime2[0];
	var endTime = dateTime2[1];
	// 转换开始时间到Date类
	var formateStartDate = startDate.split("-");
	var formateStartTime = startTime.split(":")
	var startDateTime = new Date();
	startDateTime.setFullYear(formateStartDate[0]);
	startDateTime.setMonth(formateStartDate[1]);
	startDateTime.setMonth(startDateTime.getMonth() - 1);
	startDateTime.setDate(formateStartDate[2]);
	startDateTime.setHours(formateStartTime[0]);
	startDateTime.setMinutes(formateStartTime[1]);
	startDateTime.setSeconds("0");
	// 转换结束时间到Date类
	var formateEndDate = endDate.split("-");
	var formateEndTime = endTime.split(":")
	var endDateTime = new Date();
	endDateTime.setFullYear(formateEndDate[0]);
	endDateTime.setMonth(formateEndDate[1]);
	endDateTime.setMonth(endDateTime.getMonth() - 1);
	endDateTime.setDate(formateEndDate[2]);
	endDateTime.setHours(formateEndTime[0]);
	endDateTime.setMinutes(formateEndTime[1]);
	endDateTime.setSeconds("0");
	// 计算两个时间的差值
	var timeLength = (endDateTime.getTime() - startDateTime.getTime()) / 1000 / 60;
	length = Math.round(timeLength);
	return length;
}
//检查分数 
ruite.form.compareMark = function(TotalMark, PassMark)
{
	var a = parseInt(TotalMark);
	var b = parseInt(PassMark);
	//if (a == null || b == null) return true;
	return (a >= b ? true : false);
}

//比较日期大小
ruite.form.compareDate = function(DateOne, DateTwo)
{
	var a = ruite.parse.date(DateOne);
	var b = ruite.parse.date(DateTwo);
	
	if (a == null || b == null) return false;
	
	return (a <= b);
}

//比较时间大小
ruite.form.compareTime = function(TimeOne, TimeTwo)
{
	var a = ruite.parse.timestamp(TimeOne);
	var b = ruite.parse.timestamp(TimeTwo);
	
	if (a == null || b == null) return false;
	
	return (a <= b);
}

//===========================================================================================

/** 用于指示错误的异常类 */
ruite.form.Exception = function (element, message)
{
	this.element = element;
	this.message = message;
}

//===========================================================================================

ruite.form.trimString = function (str)
{
	return str.replace(/(^\s*)|(\s*$)/g, "");
}